<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use app\admin\validate\CheckRoles;
use think\exception\ValidateException;
use think\facade\Cache;
class Roles extends Base
{

    protected $mod;
    protected function initialize()
    {
        $this->mod=\app\admin\model\Roles::class;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $dataList = $this->mod::with('admin')->select();
        return $this->success('', $dataList);
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = $this->request->param();
        try {
            //验证
            if (isset($data['id'])) {
                $tips = '更新';
            } else {
                $tips = '增加';
            }
            $valCheck = validate(CheckRoles::class)->check($data);
            if ($valCheck !== true) {
                $this->error($valCheck);
            }
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        if ($this->mod::create($data, [], true)) {
            return $this->success($tips . '成功');
        } else {
            return $this->error($tips . '失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }
    public function getroles($id = 0)
    {
        if (!$id) {
            return;
        }
        $roles = $this->mod::with('admin')->find($id);
        return $this->success('', $roles);
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    public function authupdate($rules=[])
    {
        $data = $this->request->param();
        $roles = $this->mod::find($data['id']);
        if (!$roles) {
            return $this->error('角色不存在');
        }
        if ($this->mod::update($data)) {
            return $this->success('权限分配成功');
            Cache::clear();
        } else {
            return $this->error('权限分配失败');
        };
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id) {
            return;
        }
        $r = $this->mod::with('admin')->find($id);
        if (count($r->admin) > 0) {
            return $this->error('请先删除角色下成员再操作');
        }
        if ($this->mod::destroy($id)) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}
